package com.zby.controller.article;

import com.zby.common.utils.ResultUtil;
import com.zby.model.pojo.Article;
import com.zby.service.ArticleService;
import com.zby.vo.ArticleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Tag(name = "文章管理")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    // 增加文章
    @PostMapping("/addArticle")
    @Operation(summary = "增加文章")
    public ResultUtil<Integer> addArticle(@RequestBody Article article) {
        int article1 = articleService.addArticle(article);
        if (article1 <= 0) {
            return ResultUtil.fail();
        }
        return ResultUtil.success();
    }

    // 查询文章
    @GetMapping("/selectArticle")
    @Operation(summary = "查找文章")
    public ResultUtil<List<ArticleVo>> updateArticle() {
        List<ArticleVo> list = articleService.selectArticle();
        return ResultUtil.success(list);
    }

    // 查询文章内容
    @GetMapping("/selectContent/{id}")
    @Operation(summary = "查找文章内容")
    public ResultUtil<ArticleVo> selectArticleContent(@PathVariable("id") Integer id) {
        ArticleVo article = articleService.selectArticleContent(id);
        return ResultUtil.success(article);
    }

    // 修改文章
    @PutMapping("/updateArticle")
    @Operation(summary = "修改文章")
    public ResultUtil<Integer> updateArticle(@RequestBody Article article) {
        int affectedRows = articleService.updateArticle(article);
        if (affectedRows <= 0) {
            return ResultUtil.fail();
        }
        return ResultUtil.success();
    }

    @DeleteMapping
    @Operation(summary = "删除文章")
    public ResultUtil<Object> del(@RequestBody Integer[] ids) {
        int count = articleService.del(List.of(ids));
        return count > 0 ? ResultUtil.success() : ResultUtil.fail();
    }
    // 根据前端传入的token获取用户个人文章
    @Operation(summary = "获取个人文章")
    @GetMapping("/getPersonalArticle")
    public ResultUtil<List<ArticleVo>> getPersonalArticle( @RequestParam(value = "token") String token) {
        return articleService.getPersonalArticle(token);
    }

}